#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include "ReaderWriter.h"

int value = 0;
concurrent::ReaderWriter lock;

void *reader(void *arg) {
	while ( 1 ) {
		lock.readLock();	
		printf("%d: %d\n", (int)pthread_self(), value);
		lock.unlock();
		usleep(90000);
	}
}

int main(void) {
        pthread_t ids[5];       

        for(int i=0; i < 5; i++) {
                pthread_create(&ids[i], NULL, reader, NULL);
        }

        for(int i=0; i < 100; i++) {
		lock.writeLock();
		value++;
		printf("Value incremented to %d\n", value);
		lock.unlock();
		sleep(1);
	}

        for(int i=0; i < 5; i++) {
                pthread_join(ids[i], NULL);
        }
	return 0;
}
